/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package me.panpf.sketch.sample.vt.utils;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.media.image.DataSourceUnavailableException;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import ohos.media.photokit.metadata.AVMetadataHelper;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;

/**
 *  FileUtil
 *
 * @author hw
 * @since 2021/7/15
 */
public class FileUtil {
    /**
     * 转换文件大小
     *
     * @param fileSize 文件大小long
     * @return 文件大小
     */
    public static String formatFileSize(long fileSize) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileSize == 0) {
            return wrongSize;
        }
        if (fileSize < 1000) {
            fileSizeString = df.format((double) fileSize) + "B";
        } else if (fileSize < 1048576) {
            fileSizeString = df.format((double) fileSize / 1000) + "KB";
        } else if (fileSize < 1073741824) {
            fileSizeString = df.format((double) fileSize / 1000000) + "MB";
        } else {
            fileSizeString = df.format((double) fileSize / 1000000000) + "GB";
        }
        return fileSizeString;
    }


    /**
     * 视频文件获取第一帧PixMap
     * @param context context
     * @param fileId 视频文件的ID
     * @return PixelMap
     */
    public static PixelMap getFirstPixelMap(Context context, String fileId) {
        AVMetadataHelper avMetadataHelper = new AVMetadataHelper();
        DataAbilityHelper dataAbilityHelper = DataAbilityHelper.creator(context);
        try {
            Uri uri = Uri.appendEncodedPathToUri(AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI, fileId);
            FileDescriptor fileDescriptor = dataAbilityHelper.openFile(uri, "r");
            avMetadataHelper.setSource(fileDescriptor);
        } catch (DataSourceUnavailableException | DataAbilityRemoteException | FileNotFoundException e) {
            e.printStackTrace();
        }
        PixelMap firstPixelMap = avMetadataHelper.fetchVideoPixelMapByTime(0);
        int width = firstPixelMap.getImageInfo().size.width;
        int height = firstPixelMap.getImageInfo().size.height;
        // 压缩缩略图
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
        initializationOptions.size = new Size(width / 3, height / 3);
        initializationOptions.releaseSource = true;
        PixelMap resultPixelMap = PixelMap.create(firstPixelMap, initializationOptions);
        // 如果没有缩放，那么不回收
        if (firstPixelMap != resultPixelMap) {
            // 释放Bitmap的native像素数组
            firstPixelMap.release();
        }
        return resultPixelMap;
    }
}
